/* ----------------------------------------------------------------------------
* File: skype_commands.cpp
*
* Desc: Stuffs for handling user commands
*
* Created: 11/01/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#include "main.h"

using namespace SKYPE4COMLib;


/************************************************************************/
/* Check commands in queue and execute commands                         */
/************************************************************************/
static int CheckCommands(SkypeEnv *pSkypeEnv)
{
	//char *body = NULL;
	std::string::size_type idx;

	EnterCriticalSection(&pSkypeEnv->csCommands);
	while (pSkypeEnv->listCommands.size())
	{
		MessageInfo *msgInfo = &pSkypeEnv->listCommands.back();
#ifdef _DEBUG
		MyOutputDebugString(TEXT("skype_plugin: Check command"));
		MyOutputDebugString(TEXT("skype_plugin: body - %s"), msgInfo->body);
#endif
		std::string str(msgInfo->body);
		if ((idx = str.find_first_of("chat_topic ", 0, 11)) != std::string::npos)
		{
#ifdef _DEBUG
			MyOutputDebugString(TEXT("find \"chat_topic\" command"));
#endif
		}
		else if ((idx = str.find_first_of("chat_stat ", 0, 10)) != std::string::npos)
		{
#ifdef _DEBUG
			MyOutputDebugString(TEXT("find \"chat_stat\" command"));
#endif
		}
		else if ((idx = str.find_first_of("discuss ", 0, 8)) != std::string::npos)
		{
#ifdef _DEBUG
			MyOutputDebugString(TEXT("find \"discuss\" command"));
#endif
		}
		else
		{
#ifdef _DEBUG
			MyOutputDebugString(TEXT("unknown command"));
#endif
		}
		// command always begin at "/"
		//if (body[0] = TEXT("/"))	
		//{
		//	
		//}
		pSkypeEnv->listCommands.pop_back();
	}
	LeaveCriticalSection(&pSkypeEnv->csCommands);
	
	return SKYPE_PLUG_OK;
}


/************************************************************************/
/* Thread function for handling user commands                           */
/************************************************************************/
DWORD WINAPI Commands(LPVOID lpParam)
{
	SkypeEnv *pSkypeEnv = reinterpret_cast<SkypeEnv*>(lpParam);
	if (pSkypeEnv == NULL)
		ExitThread(0);

#ifdef _DEBUG
	MyOutputDebugString(TEXT("skype_plugin: Commands() - start"));
#endif

	SetEvent(pSkypeEnv->hStartThreadEvents[COMMANDS_THREAD_NUM]);

	// Main cycle
	while (1)
	{

		if (WaitForSingleObject(pSkypeEnv->hStopEvent, THREAD_COMMANDS_SLEEP) != WAIT_TIMEOUT)
			break;
	}


#ifdef _DEBUG
	MyOutputDebugString(TEXT("skype_plugin: Commands() - finish"));
#endif

	ExitThread(0);
}